/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.BuildingElevatorConfigChooseDTO;
import org.springblade.modules.admin.dto.EstateProgressSaveOrUpdateDTO;
import org.springblade.modules.admin.entity.EstateProgressConfigElevator;
import org.springblade.modules.admin.service.IEstateProgressConfigElevatorService;
import org.springblade.modules.admin.vo.BuildingElevatorConfigChooseVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.admin.entity.EstateProgressConfig;
import org.springblade.modules.admin.vo.EstateProgressConfigVO;
import org.springblade.modules.admin.wrapper.EstateProgressConfigWrapper;
import org.springblade.modules.admin.service.IEstateProgressConfigService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 物业端任务进度显示节点配置表 控制器
 *
 * @author ysq
 * @since 2025-08-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("/admin/estate/progress/config")
@Api(value = "物业端任务进度显示节点配置", tags = "物业端任务进度显示节点配置表接口")
public class EstateProgressConfigController extends BladeController {

	private final IEstateProgressConfigService estateProgressConfigService;

	private final IEstateProgressConfigElevatorService estateProgressConfigElevatorService;

	/**
	 * 项目-电梯-是否已配置选择列表
	 */
	@PostMapping("/choose")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "项目-电梯-已配置选择列表", notes = "项目-电梯-已配置选择列表")
	public R<List<BuildingElevatorConfigChooseVO>> choose(@RequestBody BuildingElevatorConfigChooseDTO chooseDTO ) {
		List<BuildingElevatorConfigChooseVO> chooseList = estateProgressConfigService.getChooseList(chooseDTO);
		return R.data(chooseList);
	}

	/**
	 * 项目-电梯-是否已配置选择列表（分页）
	 */
	@PostMapping("/page")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "项目-电梯-是否已配置选择列表分页", notes = "项目-电梯-是否已配置选择列表分页")
	public R<IPage<BuildingElevatorConfigChooseVO>> choosePage(@RequestBody BuildingElevatorConfigChooseDTO chooseDTO, @RequestBody Query query ) {
		IPage<BuildingElevatorConfigChooseVO> chooseList = estateProgressConfigService.getChoosePage(Condition.getPage(query), chooseDTO);
		return R.data(chooseList);
	}

	/**
	 * 新增或修改配置
	 */
	@PostMapping("/add")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "新增或修改配置", notes = "传入estateProgressConfig")
	public R<Boolean> save(@Valid @RequestBody EstateProgressSaveOrUpdateDTO estateProgressConfig) {
		// 1. 判断当前租户是否已有配置，有则获取当前配置更新，无则新增
		List<EstateProgressConfig> configurationList = estateProgressConfigService.list();
		EstateProgressConfig config;
		if(Func.isEmpty(configurationList)){
			// 1.1 新增配置
			config = new EstateProgressConfig();
		}else{
			config = configurationList.get(0);
		}
		// 1.2 当租户有多条配置，则删除多条配置，只保留一条
		if(configurationList.size() > 1){
			estateProgressConfigService.removeByIds(configurationList);
		}
		config.setPointDisplayType(estateProgressConfig.getPointDisplayType());
		config.setPartOfBuilding(estateProgressConfig.getPartOfBuilding());
		boolean result = estateProgressConfigService.saveOrUpdate(config);
		// 2. 电梯列表覆盖
		estateProgressConfigElevatorService.removeByIds(estateProgressConfigElevatorService.list().stream().map(BaseEntity::getId).collect(Collectors.toList()));
		if(Func.isNotEmpty(estateProgressConfig.getElevatorIds())){
			for(Long id : estateProgressConfig.getElevatorIds()){
				EstateProgressConfigElevator configElevator = new EstateProgressConfigElevator();
				configElevator.setElevatorId(id);
				estateProgressConfigElevatorService.save(configElevator);
			}
		}
		return R.status(result);
	}

	/**
	 * 管理端读取配置
	 * get config
	 */
	@GetMapping("/getConfig")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "管理端读取配置")
	public R<EstateProgressConfig> getConfig() {
		List<EstateProgressConfig> existList = estateProgressConfigService.list();
		EstateProgressConfig firstConfig;
		if(Func.isEmpty(existList)){
			firstConfig = new EstateProgressConfig();
			firstConfig.setPointDisplayType(-1);
			firstConfig.setPartOfBuilding(-1);
			estateProgressConfigService.save(firstConfig);
			existList = estateProgressConfigService.list();
			return R.data(existList.get(0));
		}
		if(existList.size() > 1){
			firstConfig = existList.get(0);
			// 删除剩余的配置
			estateProgressConfigService.removeByIds(existList.stream().map(BaseEntity::getId).collect(Collectors.toList()));
			estateProgressConfigService.saveOrUpdate(firstConfig);
			existList = estateProgressConfigService.list();
		}
		firstConfig = existList.get(0);
		return R.data(firstConfig);
	}

	/**
	 * 管理端读取已配置电梯列表
	 */
	@GetMapping("/getConfiguredElevatorList")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "管理端读取已配置电梯列表")
	public R<List<EstateProgressConfigElevator>> getConfiguredElevatorList() {
		return R.data(estateProgressConfigElevatorService.list());
	}







}
